import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // 全查询
        //m1();

        // 条件查询
        //m2();

        // 增
        //m3();

        //删除
        //m4();

        //修改
        //m5();



    }
    private static void m5(){
        // 1、加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2、获取连接
            Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8",
                    "akedca", "411526");

            // 3、编写sql语句
            String sql = "UPDATE sys_user SET name = ?, pass = ? WHERE id = ?";

            // 4、预处理sql
            PreparedStatement preparedStatement = conn.prepareStatement(sql);

            // 5、如果有参数，设置参数
            preparedStatement.setString(1, "lisi");
            preparedStatement.setString(2, "777");
            preparedStatement.setInt(3, 2);

            // 6、执行sql语句
            int result = preparedStatement.executeUpdate();

            // 7、判断操作是否成功
            if (result > 0) {
                System.out.println("修改成功");
            } else {
                System.out.println("没有找到对应记录或修改失败");
            }

            // 8、关闭资源（注意：在实际使用中，应该在finally块中关闭所有资源）
            // 这里为了简化示例，没有包含关闭资源的代码

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private static void m4() {
        //1、 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2、获取连接
            Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8",
                    "akedca","411526");

            //3、编写sql语句
            String sql = "delete from sys_user where id = ? ";

            //4、预处理sql  .var
            PreparedStatement preparedStatement = conn.prepareStatement(sql);

            //5、如果有参数，设置参数
            preparedStatement.setObject(1,"3");

            //6、执行sql语句
            int result = preparedStatement.executeUpdate();

            // 7、如果是查询就遍历结果/如果是增删改 判断是否成功
            if(result > 0){
                System.out.println("删除成功");
            }

            // 8、返回视图

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
ement.setObject(1,"3");

    //6、执行sql语句
    int result = preparedStatement.executeUpdate();

    // 7、如果是查询就遍历结果/如果是增删改 判断是否成功
        if(result > 0){
        System.out.println("删除成功");
    }

    // 8、返回视图

} catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
            }
private static void m3() {
    //1、 加载驱动
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2、获取连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8",
                "akedca","411526");

        //3、编写sql语句
        String sql = "insert into sys_user(name,pass) values (?,?) ";

        //4、预处理sql  .var
        PreparedStatement preparedStatement = conn.prepareStatement(sql);

        //5、如果有参数，设置参数
        preparedStatement.setObject(1,"zhangsan");
        preparedStatement.setObject(2,"666");

        //6、执行sql语句
        int result = preparedStatement.executeUpdate();

        // 7、如果是查询就遍历结果/如果是增删改 判断是否成功
        if(result > 0){
            System.out.println("新增成功");
        }

        // 8、返回视图

    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
private static void m2() {
    // 键盘录入字符
    Scanner sc = new Scanner(System.in);
    System.out.println("请输入你要查询的字符：");
    String word = sc.next();

    //1、 加载驱动
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2、获取连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8",
                "akedca","411526");

        //3、编写sql语句
        String sql = "select * from sys_user where name like ?";

        //4、预处理sql  .var
        PreparedStatement preparedStatement = conn.prepareStatement(sql);

        //5、如果有参数，设置参数
        preparedStatement.setObject(1,"%"+word+"%");

        //6、执行sql语句
        ResultSet resultSet = preparedStatement.executeQuery();

        // 7、如果是查询就遍历结果/如果是增删改 判断是否成功
        while (resultSet.next()) {
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            String pass = resultSet.getString(3);
            System.out.println(new StringBuilder().append(id).append("----").append(name).append("----").append(pass).toString());
        }
        // 8、返回视图

    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
private static void m1() {
    //1、 加载驱动
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2、获取连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8",
                "akedca","411526");

        //3、编写sql语句
        String sql = "select * from sys_user";

        //4、预处理sql  .var
        PreparedStatement preparedStatement = conn.prepareStatement(sql);

        //5、如果有参数，设置参数

        //6、执行sql语句
        ResultSet resultSet = preparedStatement.executeQuery();

        // 7、如果是查询就遍历结果/如果是增删改 判断是否成功
        while (resultSet.next()) {
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            String pass = resultSet.getString(3);
            System.out.println(new StringBuilder().append(id).append("----").append(name).append("----").append(pass).toString());
        }
        // 8、返回视图

    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }


}
}